using System;
using System.Collections.Generic;
using System.Text;

namespace SeeConsoleBehaviour
{
    class Program
    {
        static void Main(string[] args)
        {
            bool showKeyPresses = GetYesOrNo("Show Key-presses?", false, false);
            bool seeColors = GetYesOrNo("See colors (first)?", false, true);
            if (seeColors) {
                ShowAllColors();
            }
            if (showKeyPresses) {
                ShowKeyPresses();
            }
            bool fillTest = GetYesOrNo("Fill screen and clear test?", false, true);
            if (fillTest) {
                FillScreenAndClear();
            }
        }

        static void ShowKeyPresses()
        {
            const bool Intercept = true;
            while (true) {
                ConsoleKeyInfo cki = Console.ReadKey(Intercept);
                if (cki.KeyChar == 0) {
                    Console.Write("no char     ; ");
                } else {
                    Console.Write("char: {0,-6}; ", ShowControlCodes(cki.KeyChar));
                }
                Console.WriteLine("{0}{1}", 
                    (cki.Modifiers == 0 ? "" : cki.Modifiers.ToString() + " + "),
                    cki.Key);
            }
        }

        private static string ShowControlCodes(char ch)
        {
            int i = (int)ch;
            string txt;
            if (i < 0x20) {
                txt = string.Format("\\x{0:x4}", i);
            } else {
                txt = ch.ToString();
            }
            return txt;
        }

        private static void ShowAllColors()
        {
            ConsoleColor orig = Console.BackgroundColor;
            Console.WriteLine("orig = {0}", orig);
            for (ConsoleColor cc = 0; cc <= ConsoleColor.White; ++cc) {
                Console.BackgroundColor = cc;
                Console.WriteLine("BackgroundColor = {0}", cc);
            }
            Console.BackgroundColor = orig;
            Console.WriteLine("Ended ({0})", Console.BackgroundColor);
        }

        private static void FillScreenAndClear()
        {
            Console.WriteLine("When see all 'a's hit a key");
            while (Console.KeyAvailable) {
                ConsoleKeyInfo cki0 = Console.ReadKey(true);
            }
            Console.WindowTop = 0;
            Console.WindowLeft = 0;
            Console.Write(new string('a', Console.WindowWidth * Console.WindowHeight));
            Console.CursorTop -= 1;
            Console.CursorLeft = 0;
            Console.Write("scroll back, so this is the bottom line!");
            //
            //PAUSE
            ConsoleKeyInfo cki = Console.ReadKey(true);
            Console.Clear();
        }


        private static bool GetYesOrNo(string prompt, bool @default, bool valueIfNoConsole)
        {
            bool result;
            prompt += @default ? " [Y/n]" : " [y/N]> ";
            Console.Write(prompt);
            string line = Console.ReadLine();
            if (line != null) {
                if (line.Trim().Equals("y", StringComparison.CurrentCultureIgnoreCase)) {
                    result = true;
                } else if (line.Trim().Equals("n", StringComparison.CurrentCultureIgnoreCase)) {
                    result = false;
                } else {
                    result = @default;
                }
            } else { // No stdin!
                result = valueIfNoConsole;
            }
            return result;
        }

    }
}
